//
//  LAGOperControlPannelView.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2018/2/4.
//  Copyright © 2018年 2Bulu. All rights reserved.
//

#import "LAGOperControlPannelView.h"
#import <UIKitUtli/UIKitUtli.h>

#define IMAGE_ROATE_RADIAN_MAX M_PI*0.3

@implementation LAGOperControlPannelView

{
    UISegmentedControl *_selectionCtrl;
    UISlider *_alpaSlider;
    UISlider *_strechSlider;
    
    CGPoint _arcCenterPoint;
    double _radianRotated;
    double _arcRadius;
}

-(instancetype)initWithAlpaValue:(float)alpa withReacteMode:(LAGreactImageMapMode)mode
{
    self = [super init];
    if(self)
    {
        self.backgroundColor = [UIColor orangeColor];
        _selectionCtrl = [[UISegmentedControl alloc]initWithItems:@[NSLocalizedString(@"IMAGE",nil),NSLocalizedString(@"MAP", nil),NSLocalizedString(@"BOTH", nil)]];
        [_selectionCtrl addTarget:self action:@selector(segSelectionChanaged:) forControlEvents:UIControlEventValueChanged];
        _selectionCtrl.selectedSegmentIndex = mode;
        [self addSubview:_selectionCtrl];
        
        _alpaSlider = [[UISlider alloc] init];
        _alpaSlider.maximumValue = 1.0;
        _alpaSlider.minimumValue = 0.05;
        _alpaSlider.value = alpa;
        [_alpaSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_alpaSlider];
        
        _strechSlider = [[UISlider alloc] init];
        _strechSlider.maximumValue = 2.0;
        _strechSlider.minimumValue = 0.0;
        _strechSlider.value = 1.0;
        [_strechSlider addTarget:self action:@selector(strechValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_strechSlider];
    }
    
    return self;
}



-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _selectionCtrl.frame = CGRectMake(10, 10, _selectionCtrl.width, _selectionCtrl.height);

    _alpaSlider.frame = CGRectMake(0, 0, self.width*0.8, _alpaSlider.height);
    _alpaSlider.center = CGPointMake(self.width/2, _selectionCtrl.bottom+20+_alpaSlider.height/2);
    
    _strechSlider.frame = CGRectMake(0, 0, self.width*0.8, _alpaSlider.height);
    _strechSlider.center = CGPointMake(self.width/2, _alpaSlider.bottom+20+_strechSlider.height/2);
    
    _arcCenterPoint = CGPointMake(_selectionCtrl.right + (self.width - _selectionCtrl.right)/2, 2);
    _arcRadius = _selectionCtrl.height*2;
    [self setNeedsDisplay];
}

-(IBAction)segSelectionChanaged:(UISegmentedControl *)seg
{
    if(self.imageiInterActiveChangedBlock)
        self.imageiInterActiveChangedBlock((LAGreactImageMapMode)seg.selectedSegmentIndex);
}

-(IBAction)sliderChanged:(UISlider *)slider
{
    if(self.alpaChangeHandleBlock)
        self.alpaChangeHandleBlock(slider.value);
}

-(IBAction)strechValueChanged:(UISlider *)slider
{
    if(self.imageHeightStrechHandleBlock)
    {
        if(_selectionCtrl.selectedSegmentIndex != LAGreactImageMode)
        {
            _selectionCtrl.selectedSegmentIndex = (NSInteger)LAGreactImageMode;
            [_selectionCtrl sendActionsForControlEvents:UIControlEventValueChanged];
        }
        if(slider.value < 1.0)
        {
            CGFloat minVal = 0.5;
            self.imageHeightStrechHandleBlock(minVal+slider.value*(1-minVal));
        }
        else
        {
            self.imageHeightStrechHandleBlock(slider.value);
        }
    }
}

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGPoint cPoint = _arcCenterPoint;
    double r = _arcRadius;
    CGContextAddArc(context, cPoint.x, cPoint.y, r, M_PI_2-IMAGE_ROATE_RADIAN_MAX, M_PI_2+IMAGE_ROATE_RADIAN_MAX, NO);
    CGContextStrokePath(context);
    
    double angle = _radianRotated + M_PI_2;
    
    CGMutablePathRef mPath = CGPathCreateMutable();
    CGPathMoveToPoint(mPath, NULL, cPoint.x, cPoint.y);
    CGPathAddLineToPoint(mPath, NULL, cPoint.x+r*cos(angle), cPoint.y+r*sin(angle));
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextAddPath(context, mPath);
    CGContextStrokePath(context);

    
}

#pragma mark-


-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    CGPoint p0 = [touch previousLocationInView:self];
    
    double a1 = atan2((p0.y-_arcCenterPoint.y),(p0.x-_arcCenterPoint.x));
    double a2 = atan2((p.y-_arcCenterPoint.y),(p.x-_arcCenterPoint.x));
    double sumRadian = _radianRotated;
    sumRadian += (a2 - a1)/3;
    if(fabs(sumRadian) < IMAGE_ROATE_RADIAN_MAX)
    {
        _radianRotated = sumRadian;
        [self setNeedsDisplay];
        if(self.imageRotateHandleBlock)
        {
            if(_selectionCtrl.selectedSegmentIndex != LAGreactImageMode)
            {
                _selectionCtrl.selectedSegmentIndex = (NSInteger)LAGreactImageMode;
                [_selectionCtrl sendActionsForControlEvents:UIControlEventValueChanged];
            }
            self.imageRotateHandleBlock(_radianRotated);
        }
    }
    
}



@end
